<?php
namespace Admin\Controller;
use Common\Controller\BackendController;

class RefreshController extends BackendController {
	
	public function _initialize() {
		parent :: _initialize();
		$this -> _cate_mod = D('ItemsCate');

		$api_config = M('ItemsSite')->where(array('code' => 'itaoke'))->getField('config');
		$this->_tbconfig = unserialize($api_config);
		
	} 
	public function index() {
		$this -> display();
	} 
	public function setting() {
		if (IS_POST) {
			$bl_45 = $this -> _post('cate_id', 'trim',0);
			$bl_47 = $this -> _post('fid', 'trim');
			$bl_48 = $this -> _post('eid', 'trim');
			$bl_50 = $this -> _post('refresh_type', 'trim',0);
			F('refresh_setting', array('cate_id' => $bl_45, 'fid' => $bl_47, 'eid' => $bl_48, 'refresh_type' => $bl_50));
			$this->collect();
		} 
	} 
	public function collect() {
		if (false === ($refresh = F('refresh_setting'))) {
			$this -> ajaxReturn(0, L('illegal_parameters'));
		} 
		if (false === ($totalcoll = F('totalcoll'))) {	// 商品总数
			$totalcoll = 0;
		} 
		if (false === ($nocoll = F('nocoll'))) {			// 失效个数
			$nocoll = 0;
		} 
		if (false === ($okcoll = F('okcoll'))) {			// 正常个数
			$okcoll = 0;
		} 
		if (false === ($firstcoll = F('firstcoll'))) {
			$firstcoll = 0;
		}		
		if (false === ($lastid = F('lastid'))) {
		    $id = 0;
		}				
		$this->item_mod = M('Items');
		$refresh_type = $refresh['refresh_type'];
		if ($refresh_type==0) {		// 只刷新无券
			$where['isq']=1;
		}	
			
		if($refresh['cate_id'] !=0)	// 刷新栏目
			$where['cate_id'] = $refresh['cate_id'];
		
		$fid = $refresh['fid'];
		$eid = $refresh['eid'];
		if ($fid && $eid) {		// id范围
			if($eid !=0)
				$where['id'] = array(array('egt', $fid), array('elt', $eid));
		} 

		$where['coupon_end_time']= array('EGT',time());						//只批量处理时间正常的优惠券
		$where['status'] ='underway';
		$where['id']=array('GT',$lastid);
		
		$allcount = $this ->item_mod->where($where)->count('id');
		if ($allcount==0) {
			$this -> ajaxReturn(0, '您设置的条件下没有数据，请重新设置或清除缓存！');
		}
		
		if($firstcoll==0){
			$firstcoll=$allcount;
			F('firstcoll', $firstcoll);
		}
		
		$p = $this->_get('p', 'intval', 1);
		$pre = 1;
		$start = ($p - 1) * $pre;
	//	$start=0;
		$res = $this->item_mod->where($where)->order("id asc")->limit(1)->select();
		if($res==''){
			F('totalcoll', null);
			F('okcoll', null);
			F('nocoll', null);
			$this -> ajaxReturn(0, '刷新完成！');			
		}
		
		$lastid=$res[0]['id'];
		$tb_top = $this->_get_top();
		$req = $tb_top->load_api('ItaokeItemUpdateRequest');
		$req->putOtherTextParam('num_iid',$res[0]['num_iid']);				// 设置商品ID
		$req->putOtherTextParam('quanurl',$res[0]['quanurl']);		
		$resp = (array)$tb_top->execute($req);
		if($resp['status'] != '0000')
		{
			$this->ajaxReturn(0, $resp['msg']);
		}else{
			$_res=$resp['data'];
			if( $res[0]['isq']!=1){		// 无券
				unset($_res['quan_receive']);
				unset($_res['quan_surplus']);
			}else{
				if($_res['quan_surplus']==0){		// 过期
					$_res['status']='sellout';
					$nocoll++;
					unset($_res['quan_receive']);
				}
			}
			if($_res['volume']==''){			// 为空代表 挂了
				$_res['status']='sellout';
				$nocoll++;
				unset($_res['volume']);
			}
			
			if($_res['status']=='sellout'){		// 卖完的先记录
				$selloutids[]=$res[0]['id'];
			}
			unset($_res['quan_receive']);
			unset($_res['quan_surplus']);

			if($_res['status']!='sellout'){
			     $okcoll++;
			}
			//更新
			$this->item_mod->where(array('id'=>$res[0]['id']))->save($_res);
			$totalcoll++;
		}
		
		F('lastid', $lastid);         //记录上次刷新的ID
		F('totalcoll', $totalcoll);
		F('okcoll', $okcoll);		// 正常个数
		F('nocoll', $nocoll);		// 失效个数
		
		$bl_25['title'] = '批量刷新优惠券- 爱淘客联盟开发';
		$this -> assign('CheckItemCount', $allcount);     // 共有 多少个 每次刷新
		$this -> assign('totalcoll', $totalcoll);         // 此次刷新已刷新多少个
		$this -> assign('okcoll', $okcoll);               // 正常个数 
		$this -> assign('nocoll', $nocoll);               // 失效个数
		$bl_24 = $this->fetch('info');
		$this -> ajaxReturn(1, $bl_25, $bl_24);
	} 
	
	private function _get_top() {
		vendor('Itaoke.TopClient');
		vendor('Itaoke.RequestCheckUtil');
		vendor('Itaoke.Logger');
		$top = new \TopClient;
		$top->appkey = $this->_tbconfig['app_key'];
		$top->secretKey = $this->_tbconfig['app_secret'];
		return $top;
	}
	
	/**
	 * 用于批量更新已卖出商品
	 */
	public function updateSellout($selloutids){
		$where['id']=array('in',$selloutids);
		$_res['status']='sellout';
		$this->item_mod->where($where)->save($_res);
	}
} 
